<?php
namespace app\setting\controller;
use think\Request;
use app\common\controller\AdminBase;

class ApiAdminDomain extends AdminBase
{
    public function itemAdd()
    {
        $domain = input('post.domain');
        $httpType = input('post.httpType');
        $itemInfo = db('BaseDomain')->where('domain',$domain)->find();
        if ($itemInfo) {
            return jsonError('域名已存在，请重新输入');
        }
        db('BaseDomain')->insert(array(
            'domain' => $domain,
            'http_type' => $httpType,
        ));
        return jsonSuccess('成功');
    }
    
    public function itemEdit()
    {
        $id = input('post.id');
        $domain = input('post.domain');
        $httpType = input('post.httpType');
        if (!$domain) {
          return jsonError('请输入域名');
        }
        $itemInfo = db('BaseDomain')->where('id',$id)->find();
        if (!$itemInfo) {
          return jsonError('修改项不存在');
        }
        $itemInfo = db('BaseDomain')->where('id','<>',$id)->where('domain',$domain)->find();
        if ($itemInfo) {
          return jsonError('域名已存在，请重新输入');
        }
        db('BaseDomain')->where('id',$id)->update(array(
            'domain' => $domain,
            'http_type' => $httpType,
        ));
        return jsonSuccess('成功');
    }
    
    public function itemDataUpdate()
    {
        $id = input('post.id');
        $setting = input('post.setting/a');
        $itemInfo = db('BaseDomain')->where('id',$id)->find();
        if (!$itemInfo) {
          return jsonError('修改项不存在');
        }
        db('BaseDomain')->where('id',$id)->update(array(
            'data' => json_encode($setting),
        ));
        return jsonSuccess('成功');
    }
    
    public function itemsDel()
    {
        $ids = input('post.ids');
        if (!$ids) {
            return jsonError('缺少参数');
        }
        $ids = explode(',',$ids);
        foreach ($ids as $id) {
            $itemInfo = db('BaseDomain')->where('id',$id)->find();
            if ($itemInfo) {
                db('BaseDomain')->where('id',$id)->delete();
            }
        }
        return jsonSuccess('操作成功');
    }
    
    public function itemDel()
    {
        $id = input('post.id');
        if (!$id) {
          return jsonError('缺少参数');
        }
        $itemInfo = db('BaseDomain')->where('id',$id)->find();
        if ($itemInfo) {
            db('BaseDomain')->where('id',$id)->delete();
        }
        return jsonSuccess('操作成功');
    }
    
    public function itemInfo()
    {
        $id = input('post.id');
        $itemInfo = db('BaseDomain')->where('id',$id)->find();
        $itemInfo['data'] = json_decode($itemInfo['data']);
        return jsonSuccess('成功',$itemInfo);
    }
    
    public function itemList()
    {
        $status = input('post.status');
        $orderBy = input('post.orderBy');
        $order = input('post.order');
        $page = input('post.page');
        $limit = input('post.limit');
        $limit ? $limit : 10;
        $orderBy = $orderBy ? $orderBy : 'id';
        $order = $order ? $order : 'desc';
        $itemList = db('BaseDomain')->page($page,$limit)->order($orderBy,$order)->select();
        if ($itemList) {
            foreach ($itemList as $key => $value) {
                $itemList[$key]['data'] = json_decode($value['data']);
            }
        }
        $total = db('BaseDomain')->count();
        return jsonSuccess('',['itemList' => $itemList,'total' => $total]);
    }

}